package com.airlines.crewassignment.domain.util;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.airlines.common.DateTimeRangeUtil;
import com.airlines.common.SpringContextUtil;
import com.airlines.crewassignment.domain.Crew;
import com.airlines.crewassignment.domain.CrewFlightExperienceAcType;
import com.airlines.crewassignment.domain.CrewPositionAssignment;
import com.airlines.crewassignment.domain.DateTimeRange;
import com.airlines.crewassignment.domain.LinkLine;
import com.airlines.crewassignment.domain.Position;
import com.airlines.crewassignment.domain.Tech;
import com.airlines.crewassignment.domain.crew.CrewExamine;
import com.airlines.crewassignment.service.crew.TechService;

/**
 * @author xxxx
 * @date 2018年4月17日 
 * @description 资质类检查
 */
public class SkillUtil {
	private static TechService techService;
	
	private static TechService getTechService(){
		if(techService == null){
			techService = (TechService)SpringContextUtil.getBean(TechService.class);
		}
		return techService;
	}
	
	/**
	 * 获取某个机组人员90天起落的最小值（按机型）
	 * @param list
	 * @param crew
	 * @return
	 */
	public static int getMinUpdownCount90Days(List<CrewPositionAssignment> list,Crew crew){
		if(CollectionUtils.isEmpty(list)){
			return -1;
		}
		Set<String> acTypeSet = new HashSet<>();
		
		Set<LinkLine> linkLineSet = CrewPositionAssignmentUtil.getLinkLineSet(list);
		for(LinkLine l : linkLineSet){
			String acType = l.getAcType();
			acTypeSet.add(acType);
		}
		return getMinUpdownCount90Days(acTypeSet,crew);
	}

	/**
	 * 获取某个机组人员90天起落的最小值（按机型）
	 * @param acTypeSet
	 * @param crew
	 * @return
	 */
	public static int getMinUpdownCount90Days(Set<String> acTypeSet,Crew crew){
		int minUpdown = Integer.MAX_VALUE;
		List<CrewFlightExperienceAcType> cfeAcTypeList = crew.getCrewFlightExperience().getCfeAcTypeList();
		for(CrewFlightExperienceAcType cfeAcType : cfeAcTypeList){
			for(String acTypeLinkLine : acTypeSet){
				if(StringUtils.equals(acTypeLinkLine, cfeAcType.getAcType())){
					int updownCount = cfeAcType.getUpdownCount90Days();
					if(updownCount < minUpdown){
						minUpdown = updownCount;
					}

				}
			}
		}

		if(minUpdown ==  Integer.MAX_VALUE){
			minUpdown = 0;
		}
		return minUpdown;
	}
	
	/**
	 * 检查某个号位某个飞行员的技术授权是否符合
	 * @param position
	 * @param crew
	 * @return
	 */
	public static boolean positionSkillCk(Position position,Crew crew){
		TechService techService =  SkillUtil.getTechService();
		return techService.CrewTechIsAbleForPositionTech(crew, position);
	}
	
	/**
	 * 查询某个人是否是教员（该机型上）
	 * @param position
	 * @param crew
	 * @return
	 */
	public static boolean isTeacher(Position position,Crew crew){
		TechService techService =  SkillUtil.getTechService();
		Tech  t = techService.getTechZByPosition(crew, position.getLinkLine().getAcType());
		return techService.isTeacher(t);
	}
	
	/**
	 * 查询某个人是否是新机长（该机型上）
	 * @param position
	 * @param crew
	 * @return
	 */
	public static boolean isNewCaption(Position position,Crew crew){
		TechService techService =  SkillUtil.getTechService();
		Tech  t = techService.getTechZByPosition(crew, position.getLinkLine().getAcType());
		return techService.isNewCaption(t);
	}
	
	/**
	 * 检查飞行员所安排的号位是否符合检查、培训等有效期
	 * 如果CrewExamine 有机型，则比对相同机型下的有效期
	 * 如何全部在有效期内，则返回true，否则返回false
	 * @param position
	 * @param crew
	 * @return
	 */
	public static boolean crewExamineCk(Position position,Crew crew){
		LinkLine linkLine = position.getLinkLine();
		DateTimeRange linkLineRange = new DateTimeRange(linkLine.getTd(),linkLine.getTa());
		
		List<CrewExamine> crewExamineList  = crew.getCrewFlightExperience().getCrewExamineList();
		for(CrewExamine ce : crewExamineList){
			DateTimeRange usableDateTimeRange = ce.getUsableDateTimeRange();
			if(StringUtils.isBlank(ce.getAcType())){
				if(linkLineRange.getStart() == null||linkLineRange.getEnd() == null || usableDateTimeRange.getStart() == null
						||usableDateTimeRange.getEnd() == null){
					System.out.println("");
				}
				if(!DateTimeRangeUtil.isSubRange(linkLineRange, usableDateTimeRange)){
					return false;
				}
			}else{
				if(!DateTimeRangeUtil.isSubRange(linkLineRange, usableDateTimeRange)
						&& StringUtils.equals(linkLine.getAcType(), ce.getAcType())){
					return false;
				}
			}
		}
		return true;
	}
}
